package com.yq.ylmall.product.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.ylmall.dto.product.BannerDto;
import com.yq.ylmall.dto.product.BannerQueryDto;
import com.yq.ylmall.model.product.Banner;
import com.yq.ylmall.product.service.IBannerService;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.result.ResultCode;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * Banner控制器
 * </p>
 *
 * @author yzk789
 * @since 2024-06-17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/product/banner")
public class BannerAdminController {

    private final IBannerService bannerService;

    @ApiOperation(value = "分页条件查询Banner")
    @PostMapping("/page")
    public Result<IPage<Banner>> page(@RequestBody BannerQueryDto dto) {
        Page<Banner> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        bannerService.page(
                page,
                Wrappers.<Banner>lambdaQuery()
                        .like(StringUtils.hasText(dto.getDesc()), Banner::getDesc, dto.getDesc())
                        .eq(dto.getStatus() != null, Banner::getStatus, dto.getStatus())
                        .orderByDesc(Banner::getSort)
        );
        return Result.ok(page);
    }

    @ApiOperation(value = "修改Banner状态")
    @PostMapping("/status")
    public Result<Object> updateStatus(@RequestBody BannerDto dto) {
        boolean row = bannerService.update(Wrappers.<Banner>lambdaUpdate().set(Banner::getStatus, dto.getStatus()).eq(Banner::getId, dto.getId()));
        return row ? Result.ok(ResultCode.UPDATE_SUCCESS) : Result.fail(ResultCode.UPDATE_FAIL);
    }

    @ApiOperation(value = "Banner操作：有ID更新，无ID新增")
    @PostMapping("/operate")
    public Result<Object> operateBanner(@RequestBody BannerDto dto) {
        try {
            Banner banner = new Banner();
            BeanUtils.copyProperties(dto, banner);

            // 判断是否有ID，有ID则执行更新操作，否则执行新增操作
            if (dto.getId() != null) {
                boolean updateSuccess = bannerService.updateById(banner);
                return updateSuccess ? Result.ok(ResultCode.UPDATE_SUCCESS) : Result.fail(ResultCode.UPDATE_FAIL);
            } else {
                boolean saveSuccess = bannerService.save(banner);
                return saveSuccess ? Result.ok(ResultCode.SAVE_SUCCESS) : Result.fail(ResultCode.SAVE_FAIL);
            }
        } catch (Exception e) {
            return Result.fail(ResultCode.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("/remove/{id}")
    public Result<Object> delBannerById(@PathVariable Long id) {
        boolean deleted = bannerService.removeById(id);
        return deleted ? Result.ok(ResultCode.DELETE_SUCCESS) : Result.fail(ResultCode.DELETE_FAIL);
    }

    @ApiOperation(value = "批量删除Banner")
    @DeleteMapping("/remove/batch")
    public Result<Object> delBannerByIds(@RequestBody List<Long> ids) {
        boolean deleted = bannerService.removeByIds(ids);
        return deleted ? Result.ok(ResultCode.DELETE_SUCCESS) : Result.fail(ResultCode.DELETE_FAIL);
    }

}
